//= require echarts.min

var HOME_OBJ = {
    EChart: echarts.init(document.getElementById('main-chart')),
    START_TIME: getDateBeforeSeveralDays(getDateToday(),29),
    DATE_ARR: [],
    ROOM_NUM_ARR:   new Array(30),
    DINNER_NUM_ARR: new Array(30),
    OPTION: {}
}

HOME_OBJ.getEChart = function () {
    HOME_OBJ.getDateArray()                     // 获取30天时间(X轴)
    HOME_OBJ.getRoomNums()                      // 获取房型订单数据
    HOME_OBJ.OPTION = HOME_OBJ.option           // 获取图表参数
    HOME_OBJ.EChart.setOption(HOME_OBJ.OPTION)  // 使用指定的配置项和数据显示图表
}

jQuery(function ($) {
    if (getPageName() == "HOME-MAIN-PAGE") {
        HOME_OBJ.getEChart()    // 渲染曲线
        // HOME_OBJ.getSmsSurplusNumber()              //短信剩余数量
    }

})

// 获取30天时间(X轴)
HOME_OBJ.getDateArray = function() {
    for (var i = 0; i < 30; i++) {
        HOME_OBJ.DATE_ARR[i] = getDateAfterSeveralDays(HOME_OBJ.START_TIME,i).substring(5,10)
    }
    return HOME_OBJ.DATE_ARR
}

// 获取订单数据
HOME_OBJ.getRoomNums = function () {
    for (var i = 0; i < 30; i++) {
        HOME_OBJ.ROOM_NUM_ARR[i] = 0
    }
    $.ajax({
        url: "/api/store_orders/data",
        type: "get",
        dataType: 'json',
        async: false,
        success: function(data) {
            var length = data.store_orders.length
            if (length > 0) {
                var creat_times = []
                var nums = []
                creat_times.push(data.store_orders[0].substring(5,10))
                for (var i = 1; i < length; i++) {
                    var time_before = data.store_orders[i-1].substring(5,10)
                    var time_after = data.store_orders[i].substring(5,10)
                    if (time_before != time_after) {
                        creat_times.push(data.store_orders[i].substring(5,10))
                    }
                }
                for (var i = 0; i < creat_times.length; i++) {
                    var n = 1
                    for (var j = 0; j < length; j++) {
                        if (creat_times[i] == data.store_orders[j].substring(5,10)) {
                            nums[i] = n++
                        }
                    }
                }
                for (var i = 0; i < creat_times.length; i++) {
                    HOME_OBJ.ROOM_NUM_ARR[HOME_OBJ.getDateArray().indexOf(creat_times[i])] = nums[i]
                }
            } else {
                return false
            }
        },
        error: function() {
            commonEvents.popupView("提 醒", "获取订单数据失败", "确 定")
        }
    })
}

// 获取图表参数
HOME_OBJ.option = {
    title: {
        text: ''
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data:['订单']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: HOME_OBJ.getDateArray()
    },
    yAxis: {
        type: 'value'
    },
    series: [ // 加载顺序
        {
            name:'订单',
            type:'line',
            stack: '总量',
            data: HOME_OBJ.ROOM_NUM_ARR
        }
    ]
}
